package com.guojian.jdbc.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.Assert;

import com.guojian.jdbc.JdbcDataFormat;

/**
 * 
 * 数据转换组合 Mapper
 * 
 * @author GuoJian
 * 
 * @param <T>
 */
public class DataFormatRowMapper<T> implements RowMapper<T> {

	private JdbcDataFormat<T> format;

	private RowMapper<T> rowMapper;

	public DataFormatRowMapper(JdbcDataFormat<T> format, RowMapper<T> rowMapper) {
		Assert.notNull(format, "JdbcDataFormat must not be null");
		Assert.notNull(rowMapper, "RowMapper must not be null");
		this.format = format;
		this.rowMapper = rowMapper;
	}

	@Override
	public T mapRow(ResultSet rs, int rowNum) throws SQLException {
		T row = rowMapper.mapRow(rs, rowNum);
		format.formatRow(row);
		return row;
	}

}
